# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging

from odoo import _, api, models
from odoo.exceptions import ValidationError
from .zhifubaoqingqiu import zhifubaoqingqiu


from odoo.addons.shilimokuai001.controllers.controllers import MyController
_logger = logging.getLogger(__name__)


class PaymentTransaction(models.Model):
    _inherit = 'payment.transaction'
    def _get_specific_processing_values(self, processing_values):

        # 调用父类的方法，获取处理值
        res = super()._get_specific_processing_values(processing_values)
        # 如果支付方式不是支付宝，则返回处理值
        if self.provider_code != 'zhifubao':
            return res
        processing_values["return_url"]=MyController._return_url
        processing_values["notify_url"]=MyController._webhook_url
        res.update(processing_values)
        res.update({
            "redirect_form_html":zhifubaoqingqiu(processing_values),
            
        }
        )
        
        return res
    def _get_tx_from_notification_data(self, provider_code, notification_data):
        """ Override of payment to find the transaction based on custom data.

        :param str provider_code: The code of the provider that handled the transaction
        :param dict notification_data: The notification feedback data
        :return: The transaction if found
        :rtype: recordset of `payment.transaction`
        :raise: ValidationError if the data match no transaction
        """
        tx = super()._get_tx_from_notification_data(provider_code, notification_data)
        if provider_code != 'zhifubao' or len(tx) == 1:
            return tx
        # 调用父类的方法，获取交易

        # 如果支付方式不是支付宝，或者只有一个交易，则返回交易
        reference = notification_data.get('out_trade_no')
        tx = self.search([('reference', '=', reference), ('provider_code', '=', 'zhifubao')])
        if not tx:
        # 获取交易参考号
            raise ValidationError(
        # 根据交易参考号和支付方式搜索交易
                "Wire Transfer: " + _("No transaction found matching reference %s.", reference)
        # 如果没有找到交易，则抛出异常
            )
        return tx

    def _process_notification_data(self, notification_data):
        """ Override of payment to process the transaction based on custom data.

        Note: self.ensure_one()

        :param dict notification_data: The custom data
        :return: None
        """
        super()._process_notification_data(notification_data)
        if self.provider_code != 'zhifubao':
            return
        # 调用父类的方法，处理通知数据

        # 如果支付方式不是支付宝，则返回
        _logger.info(
            "validated custom payment for transaction with reference %s: set as pending",
            self.reference
        # 记录日志，验证自定义支付交易，设置为待处理
        )
        self._set_done()        # 设置交易为已完成
